﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-11>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：打印通知单
//********************************************************************************//
layui.use(['form', 'jquery', 'laydate'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , laydate = layui.laydate;
    /*initial load start*/
    initcontrol();
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        //加载主管部门
        $("#cBM option").remove();
        var list = getQSBMList("");
        for (var i = 0; i < list.length; i++) {
            $("#cBM").append("<option value='" + list[i]["Value"] + "'>" + list[i]["Text"] + "</option>");
        }
        $("#cBM").get(0).selectedIndex = 0;
        form.render("select");
        //设置默认打印日期与回执日期
        var date = new Date()
        $("#printdate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
        date.setMonth(date.getMonth() + 1);
        $("#returndate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
        //设置界面元素默认显示
        $("table[name='ContentList']").hide();
    }
    /*end initial*/

    /*event*/
    $("#leftmenu1 tr").click(function () {
        //清除所有背景色,设置当前选中行背景色
        $("#leftmenu1 tr").css("background-color", "");
        $("#leftmenu1 tr").removeAttr("selected");
        $(this).css("background-color", "gray");
        $(this).attr("selected", "selected");
        //隐藏所有列表
        $("table[name='ContentList']").hide();
        //显示当前选择
        //$("#ContentList" + $(this).attr("vid") + " .datarow").remove();
        $("#ContentList" + $(this).attr("vid")).show();
        //显示当前列表记录条数
        var count = $("#ContentList" + $(this).attr("vid")).find(".datarow").length;
        $("#lsCount").text(count);
    });
    $("#oSearch").click(function () {
        var ym = $("#cYM").val();
        var sl = $("#cSL").val();
        var bm = $("#cBM").val();
        var printdate = $("#printdate").val();
        var returndate = $("#returndates").val();
        var tr = $("#leftmenu1 tr[selected='selected']");
        if (tr.length == 0) {
            layer.alert("请在左侧选择一项通知书");
            return;
        }
        var type = tr.attr("vid");
        if ((type == "1" || type == "2" || type == "7" ) && ym == "") {
            layer.alert("请输入水量年(月)");
            return;
        }
        //用户邮寄地址不需要年月,随便设个0
        if (type == "5")
        {
            ym = 0;
        }
        load(type, ym, sl, bm, printdate, returndate);
    });

    //按用户号定位
    $("#oGoTo").click(function () {
        //清除旧定位
        $("table[name='ContentList']:visible").find(".datarow[dw='1']").css("background-color", "");
        $("table[name='ContentList']:visible").find(".datarow[dw='1']").removeAttr("dw");
        var yhh = $.trim($("#cYHH").val());
        if (yhh != "") {            
            //获取当前显示的列表
            var tab = $("table[name='ContentList']:visible");
            if ($(tab).find(".datarow").length > 0) {
                //获取所有用户号列
                var tds = $(tab).find("td[name='yhh']");
                //获取匹配项
                //$(tds).each(function () {
                //    $(this).parent().css("background-color", "");
                //});
                $(tds).each(function () {
                    if ($(this).text().indexOf(yhh) != -1) {
                        $(this).parent().css("background-color", "gray");
                        $(this).parent().attr("dw","1");
                        var top = $(this).offset().top;
                        if (top > 620) {
                            //滚动条到该位置
                            $(document).scrollTop(top - 250);
                        }
                        return false
                    }
                });
            }
        }
    });

    //临听全选/取消
    form.on("checkbox(selectAll)", function (data) {
        vid = $(data.elem).attr("vid");
        if (data.elem.checked) {
            $("#ContentList"+vid+" .datarow").find("[name='liCheck']").prop("checked", "checked");
        }
        else {
            $("#ContentList" + vid + " .datarow").find("[name='liCheck']").removeProp("checked");
        }
        form.render("checkbox");
    });
    //临听选多少条
    form.on("checkbox(selectCon)", function (data) {
        var checks = $("table[name='ContentList']:visible").find("[name='liCheck']:checked");
        if (data.elem.checked && $("#cCount").val() != "" && $("table[name='ContentList']:visible").find(".datarow").length>0)
        {            
            var firsttr = "";
            //判断是否有定位,有定位则从定位开始选,无则从当前选中项第一条开始选
            var dw = $("table[name='ContentList']:visible").find(".datarow[dw='1']");
            if ($(dw).length == 1)
            {
                firsttr = $(dw);
            }
            else
            {
                if ($(checks).length > 0) {
                    firsttr = $(checks).eq(0).parent().parent();
                }
                else {
                    firsttr = $("table[name='ContentList']:visible").find(".datarow").eq(0);
                }
            }
            $("table[name='ContentList']:visible").find("[name='liCheck']").each(function () {
                $(this).removeProp("checked");
            });
            for (var i = 0; i < parseInt($("#cCount").val()); i++) {
                $(firsttr).find("[name='liCheck']").prop("checked", "checked");
                firsttr = $(firsttr).next();
            }
        }
        else if (!data.elem.checked && $("#cCount").val() != "" && $("table[name='ContentList']:visible").find(".datarow").length > 0)
        {
            if ($(checks).length > 0) {
                for (var i = 0  ; i < parseInt($("#cCount").val()) ; i++) {
                    $(checks).eq($(checks).length - 1 - i).removeProp("checked");
                }
            }
        }
        form.render("checkbox");
    });
    
    //条件选中
    $("#oConditionSelect").click(function () {
        if ($("[name='cSelect']:checked").val() == "1")
        {
            var yhh=$.trim($("#cSelect1con1").val());
            if (yhh != "")
            {                
                $("table[name='ContentList']:visible").find(":checkbox").removeProp("checked");
                var matchTr = $("table[name='ContentList']:visible").find("tr[vid='" + yhh + "']");
                if (matchTr.length == 1)
                {
                    $(matchTr).find(":checkbox").prop("checked", "checked");
                    $(matchTr).nextAll().find(":checkbox").each(function () {
                        $(this).prop("checked", "checked");
                    });
                    form.render("checkbox");
                }
            }
            
        }
        else if ($("[name='cSelect']:checked").val() == "2")
        {
            var yhh = $.trim($("#cSelect2con1").val());
            var count = $.trim($("#cSelect2con2").val());
            if (yhh != ""&&count!=""&&parseInt(count)>=1) {
                $("table[name='ContentList']:visible").find(":checkbox").removeProp("checked");
                var matchTr = $("table[name='ContentList']:visible").find("tr[vid='" + yhh + "']");
                if (matchTr.length == 1) {
                    $(matchTr).find(":checkbox").prop("checked", "checked");
                    if (parseInt(count) > 1) {
                        var i = 1;
                        $(matchTr).nextAll().find(":checkbox").each(function () {
                            if (i <= count - 1) {
                                $(this).prop("checked", "checked");
                                i=i+1;
                            }
                            else {
                                return false;
                            }
                        });
                    }
                    form.render("checkbox");
                }
            }
        }
    });
    //打印
    $("#oPrint").click(function () {
        var checks = $("table[name='ContentList']:visible").find("[name='liCheck']:checked");
        if(checks.length==0)
        {
            return;
        }
        var yhhArr = new Array();
        $(checks).each(function () {
            yhhArr.push($(this).attr("vid"));
        });
        //取得报表参数 
        var type = $("#leftmenu1 tr[selected='selected']").attr("vid");
        var bbid = "";
        bbid = (type == "1") ? 100205 : (type == "2") ? 100206 : (type == "5") ? 100207 : (type == "7") ? 100209 : (type == "8") ? 120005 : -1;
        bbtitle=(type == "1") ? "采灌井计划水量通知书" : (type == "2") ?"开采井计划水量通知书": (type == "5") ? "用户邮寄地址打印" : "";
        var ym = $("#cYM").val();
        var sl = $("#cSL").val();
        var bm = $("#cBM").val();
        var printdate = $("#printdate").val();
        var returndate = $("#returndate").val();
        var params = new Array();
        params[0] = "V_TYPE:" + type;
        params[1] = "V_YM:" + ym;
        params[2] = "V_SL:" + sl;
        params[3] = "V_BM:" + bm;
        params[4] = "V_PRINTDATE:" + printdate;
        params[5] = "V_RETURNDATE:" + returndate;
        params[6] = "V_YONGHUHS:" + yhhArr.join(",");
        wap.common.printjx('ZhangDanDY', [
            { key: 'QUERYPARAM', value: params.join("|") },
            { key: 'BAOBIAOID', value: bbid },
            { key: 'USERINFO', value: "csms" }
        ]);
        
        //var url = "/Pages/YongShui/15.BaoBiaoGL/BaoBiaoView.aspx?baobiaoid=" + bbid + "&baobiaocs=" + params.join("|");
        //layer.open({
        //    type: 2,
        //    title: bbtitle,
        //    shadeClose: false,
        //    shade: 0.8,
        //    area: ['1100px', '660px'],
        //    content:url
        //});
    });

    /*event end*/

    /*list*/
    function createItemStr(type, obj) {
        var str = $("#ItemStr" + type + " tr").prop("outerHTML");
        var itemstr, yhh, yhm, ym, dz, yb, kcjh, hgjh,cwbm;
        yhh = obj["S_YONGHUH"];
        yhm = obj["S_YONGHUM"];
        ym = obj["I_YM"];
        dz = obj["S_DIZHI"];
        yb = obj["S_YOUBIAN"];
        cwbm = obj["S_CAIWUBM"];
        itemstr = str.replace(new RegExp("objid", "gm"), yhh)
                        .replace(new RegExp("objYongHuH", "gm"), yhh)
                        .replace(new RegExp("objYongHuM", "gm"), yhm)
                        .replace(new RegExp("objDiZhi", "gm"), dz)
                        .replace(new RegExp("objYouBian", "gm"), yb)
                        .replace(new RegExp("objYM", "gm"), ym)                        
        ;
        if (type == 1) {
            kcjh = obj["I_KAICAIJH"];
            hgjh = obj["I_HUIGUANJH"];
            itemstr = itemstr.replace(new RegExp("objKaiCaiJH", "gm"), kcjh).replace(new RegExp("objHuiGuanJH", "gm"), hgjh);
        }
        else if (type == 2) {
            kcjh = obj["I_KAICAIJH"];
            itemstr = itemstr.replace(new RegExp("objKaiCaiJH", "gm"), kcjh);
        }
        else if (type == 3) {
        }
        else if (type == 4) {
        }
        else if (type == 5) {
        }
        else if (type == 6) {
        }
        else if (type == 7) {
            itemstr = itemstr.replace(new RegExp("objKaiCaiL", "gm"), obj["I_KAICAIL"]);
            itemstr = itemstr.replace(new RegExp("objYear", "gm"), parseInt(ym / 100));
            itemstr = itemstr.replace(new RegExp("objMonth", "gm"), ym % 100);
            itemstr = itemstr.replace(new RegExp("objCWBM", "gm"), cwbm);
        }
        else if (type == 8) {
                itemstr = itemstr.replace(new RegExp("objKaiCaiL", "gm"), obj["I_KAICAIL"]);
                itemstr = itemstr.replace(new RegExp("objYear", "gm"), parseInt(ym / 100));
                itemstr = itemstr.replace(new RegExp("objCWBM", "gm"), cwbm);
        }
    return itemstr;
    }
    /**
    (type == "1")//采灌井计划水量
    (type == "2")//开采井计划水量
    (type == "3")//采灌井补收水费计划水量
    (type == "4")//采灌井退费
    (type == "5")//用户邮寄地址
    (type == "6")//超计划加价用水
    (type == "7")//深井水水费缴纳
    **/
    function load(type, ym, sl, bm, printdate, returndate) {
        var index = layer.load(1);
        var op = "";
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/QSQT/TongZhiS.ashx",
            data: {
                "op": op,
                "type": type,
                "ym": ym,
                "sl": sl,
                "bm": bm,
                "printdate": printdate,
                "returndate": returndate
            },
            dataType: "json",
            success: function (rs) {

                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr(type,rs[i]);
                }
                $("#lsCount").text(rs.length);
                $("#ContentList" + type).find(".datarow").remove();
                $("#ContentList" + type).append(strs);
                bindListOperate(type);
                form.render("checkbox");
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }
    function bindListOperate(type)
    {
        if(type=="7")
        {
            $("#ContentList" + type+" .datarow").find("[name='liEdit']").click(function () {
                $(this).parent().find("[name='liCWBM_S'],[name='liEdit']").hide();
                $(this).parent().find("[name='liCWBM_E'],[name='liOK'],[name='liCancel']").show();
            });
            $("#ContentList" + type + " .datarow").find("[name='liCancel']").click(function () {
                var oldvalue = $(this).parent().attr("vid");
                $(this).parent().find("[name='liCWBM_E']").val(oldvalue);
                $(this).parent().find("[name='liCWBM_E'],[name='liOK'],[name='liCancel']").hide();
                $(this).parent().find("[name='liCWBM_S'],[name='liEdit']").show();
            });
            $("#ContentList" + type + " .datarow").find("[name='liOK']").click(function () {
                //更新用户财务部门
                var yhh = $(this).parent().parent().find("td").eq(3).text();
                var newvalue = $(this).parent().find("[name='liCWBM_E']").val();
                var td=$(this).parent();
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSQT/TongZhiS.ashx",
                    data: {
                        "op": "UPDATECAIWUBM",
                        "yhh": yhh,
                        "cwbm": newvalue
                    },
                    dataType: "json",
                    success: function (rs) {
                        layer.msg(rs.Msg);
                        if (rs.State) {                            
                            $(td).find("[name='liCWBM_S']").text(newvalue);
                            $(td).attr("vid", newvalue);
                            $(td).find("[name='liCWBM_E'],[name='liOK'],[name='liCancel']").hide();
                            $(td).find("[name='liCWBM_S'],[name='liEdit']").show();
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
                
            });
        }
        else if (type == "8") {
            $("#ContentList" + type + " .datarow").find("[name='liEdit']").click(function () {
                $(this).parent().find("[name='liCWBM_S'],[name='liEdit']").hide();
                $(this).parent().find("[name='liCWBM_E'],[name='liOK'],[name='liCancel']").show();
            });
            $("#ContentList" + type + " .datarow").find("[name='liCancel']").click(function () {
                var oldvalue = $(this).parent().attr("vid");
                $(this).parent().find("[name='liCWBM_E']").val(oldvalue);
                $(this).parent().find("[name='liCWBM_E'],[name='liOK'],[name='liCancel']").hide();
                $(this).parent().find("[name='liCWBM_S'],[name='liEdit']").show();
            });
            $("#ContentList" + type + " .datarow").find("[name='liOK']").click(function () {
                //更新用户财务部门
                var yhh = $(this).parent().parent().find("td").eq(3).text();
                var newvalue = $(this).parent().find("[name='liCWBM_E']").val();
                var td = $(this).parent();
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSQT/TongZhiS.ashx",
                    data: {
                        "op": "UPDATECAIWUBM",
                        "yhh": yhh,
                        "cwbm": newvalue
                    },
                    dataType: "json",
                    success: function (rs) {
                        layer.msg(rs.Msg);
                        if (rs.State) {
                            $(td).find("[name='liCWBM_S']").text(newvalue);
                            $(td).attr("vid", newvalue);
                            $(td).find("[name='liCWBM_E'],[name='liOK'],[name='liCancel']").hide();
                            $(td).find("[name='liCWBM_S'],[name='liEdit']").show();
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });

            });
        }
    }
    /*end list*/

    /*logic*/
    function verify() {
        return true;
    }

    /*logic end*/
});
